package com.yoyo.yyadsdk.widget;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;

import androidx.appcompat.widget.AppCompatImageView;

public class DragView extends AppCompatImageView {
    private int startX, startY;
    private int offsetX, offsetY;
    private int screenWidth;
    private int initEndX, initStartX;
    private boolean isCorrect;
    private boolean move;
    private WindowManager wm;
    private WindowManager.LayoutParams wmParams;

    public DragView(Context context) {
        this(context, null);
    }

    public DragView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }

    public void setWmParams(WindowManager.LayoutParams wmParams, int screenWidth) {
        this.wmParams = wmParams;
        this.screenWidth = screenWidth;
        if (wmParams.x > screenWidth / 2) {
            initEndX = wmParams.x;
            initStartX = screenWidth - wmParams.x;
        } else {
            initStartX = wmParams.x;
            initEndX = screenWidth - wmParams.x;
        }
        isCorrect = true;
        Log.e(TAG, wmParams.x + "___________" + wmParams.y);
        Log.e(TAG, initStartX + "___________" + initEndX);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: // 捕获手指触摸按下动作
                // 获取相对View的坐标，即以此View左上角为原点
                startX = (int) event.getRawX();
                startY = (int) event.getRawY();
                if (isCorrect) {
                    isCorrect = false;
                    offsetX = wmParams.x - startX;
                    offsetY = wmParams.y - startY;
                }
                move = false;
                break;
            case MotionEvent.ACTION_MOVE://捕获手指触摸移动动作
                if (Math.abs(x - startX) < 20f && Math.abs(y - startY) < 20f) {
                    break;
                }
                updateViewPosition(x + offsetX, y + offsetY);
                move = Math.abs(x - startX) > 2f && Math.abs(y - startY) > 2f;
                break;
            case MotionEvent.ACTION_UP://捕获手指触摸离开动作
                if (Math.abs(x - startX) < 4f && Math.abs(y - startY) < 4f && !move) {
                    performClick();
                }
                updateViewPosition(x > screenWidth / 2 ? initEndX : 0, y + offsetY);
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    private static final String TAG = "DragView";

    private void updateViewPosition(int x, int y) {
        //更新浮动窗口位置参数
        wmParams.x = x;
        wmParams.y = y;
        wm.updateViewLayout(this, wmParams);//刷新显示
    }
}
